package com.supcon.exchangedata.service.rq;


import com.supcon.exchangedata.pojo.entity.mysql.operation.rq.GasLiquidInfo;
import com.supcon.exchangedata.pojo.entity.mysql.operation.rq.GasNaturalInfo;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.repository.master.MasterStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave1.SlaveOneStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave5.SlaveGasNaturalInfo;
import com.supcon.exchangedata.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class GasNaturalInfoService {
    @Resource
    private SlaveGasNaturalInfo slaveGasNaturalInfo;

    @Resource
    private MasterStatistOtherParametersDayRepository dayRepository;

    @Resource
    private SlaveOneStatistOtherParametersDayRepository day;

    /**
     * 上游分配量(万立方米) 30029
     */
    private static String gasUpstreamAllocation="30029";

    /**
     * 管道气供气量(万立方米) 30027
     */
    private static String gasPipeAmount="30027";

    /**
     * 主城区天然气销售量(万立方米) 30011
     */
    private static String zcqSalesVolume="30011";

    /**
     * 余杭区天然气销售量(万立方米) 30013
     */
    private static String yhqSalesVolume="30013";

    /**
     * 富阳区天然气销售量(万立方米) 30015
     */
    private static String fyqSalesVolume="30015";

    /**
     * 桐庐县天然气销售量(万立方米) 30017
     */
    private static String tlxSalesVolume="30017";

    /**
     * 淳安县天然气销售量(万立方米) 30024
     */
    private static String caxSalesVolume="30024";

    /**
     * 下沙区域天然气销售量(万立方米) 30026
     */
    private static String xsqySalesVolume="30026";

    /**
     * 天然气总销售量(万立方米) 30002
     */
    private static String gasAllSalesVolume="30002";

    /**
     * 上游需求量(万立方米) 30030
     */
    private static String gasUpstreamDemand="30030";

    /**
     * 总供气量(万立方米) 30008
     */
    private static String gasSupplyAmount="30008";

    /**
     * 滨江区天然气销售量(万立方米) 30012
     */
    private static String bjqSalesVolume="30012";

    /**
     * 临安区天然气销售量(万立方米) 30016
     */
    private static String laqSalesVolume="30016";

    /**
     * 建德市天然气销售量(万立方米) 30018
     */
    private static String jdsSalesVolume="30018";

    /**
     * 下沙电厂天然气销售量(万立方米) 30025
     */
    private static String xsdcSalesVolume="30025";

    /**
     * 其他天然气销售量(万立方米) 30019
     */
    private static String otherSalesVolume="30019";

    public Map<String, Double> handleData(List<StatistOtherParametersDay> otherHours) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        HashMap<String, Double> objectObjectHashMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(otherHours)) {
            otherHours.forEach((e) -> {
                LocalDateTime collectTime = e.getLastTime();
                String key = timeFormatter.format(collectTime);
                Double inAvgValue = e.getInAvgValue();
                objectObjectHashMap.put(key, inAvgValue);

            });
        }
        return objectObjectHashMap;
    }
    public void newNaturalInfo(){
        LocalDateTime localDateTime = LocalDate.now().atStartOfDay();
        for (int i = 0; i < 7; i++) {
            LocalDateTime dateTimeTarget = localDateTime.minusDays(i);
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            GasNaturalInfo topData = slaveGasNaturalInfo.findByDataTime(dateTimeTarget);
            if (topData==null){
                StatistOtherParametersDay one1 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(gasUpstreamAllocation));
                StatistOtherParametersDay one2 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(gasPipeAmount));
                StatistOtherParametersDay one3 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(zcqSalesVolume));
                StatistOtherParametersDay one4 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(yhqSalesVolume));
                StatistOtherParametersDay one5 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(fyqSalesVolume));
                StatistOtherParametersDay one6 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(tlxSalesVolume));
                StatistOtherParametersDay one7 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(caxSalesVolume));
                StatistOtherParametersDay one8 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(xsqySalesVolume));
                StatistOtherParametersDay one9 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(gasAllSalesVolume));
                StatistOtherParametersDay one10 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(gasUpstreamDemand));
                StatistOtherParametersDay one11 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(gasSupplyAmount));
                StatistOtherParametersDay one12 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(bjqSalesVolume));
                StatistOtherParametersDay one13 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(laqSalesVolume));
                StatistOtherParametersDay one14 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(jdsSalesVolume));
                StatistOtherParametersDay one15 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(xsdcSalesVolume));
                StatistOtherParametersDay one16 = day.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, Long.valueOf(otherSalesVolume));
                if ((one1 == null || one1.getInAvgValue() == null) && (one2 == null || one2.getInAvgValue() == null) &&
                        (one3 == null || one3.getInAvgValue() == null)  && (one4 == null || one4.getInAvgValue() == null)
                        && (one5 == null || one5.getInAvgValue() == null)  && (one6 == null || one6.getInAvgValue() == null)
                        && (one7 == null || one7.getInAvgValue() == null)  && (one8 == null || one8.getInAvgValue() == null)
                        && (one9 == null || one9.getInAvgValue() == null) && (one10 == null || one10.getInAvgValue() == null)  && (one11 == null || one11.getInAvgValue() == null)
                        && (one12 == null || one12.getInAvgValue() == null)  && (one13 == null || one13.getInAvgValue() == null)
                        && (one14 == null || one14.getInAvgValue() == null) && (one15 == null || one15.getInAvgValue() == null)
                        && (one16 == null || one16.getInAvgValue() == null)){
                    continue;
                }
                GasNaturalInfo gasNaturalInfo = new GasNaturalInfo();
                Timestamp timestamp = DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now());
                gasNaturalInfo.setTongTime(timestamp);
                gasNaturalInfo.setBizTime(DateTimeUtils.localDateTimeToTimeSTamp(dateTimeTarget));
                gasNaturalInfo.setOp("insert");
                gasNaturalInfo.setDataTime(dateTimeTarget);
                gasNaturalInfo.setDay(timeFormatter.format(dateTimeTarget));
                gasNaturalInfo.setGasUpstreamAllocation(one1.getInAvgValue());
                gasNaturalInfo.setGasPipeAmount(one2.getInAvgValue());
                gasNaturalInfo.setZcqSalesVolume(one3.getInAvgValue());
                gasNaturalInfo.setYhqSalesVolume(one4.getInAvgValue());
                gasNaturalInfo.setFyqSalesVolume(one5.getInAvgValue());
                gasNaturalInfo.setTlxSalesVolume(one6.getInAvgValue());
                gasNaturalInfo.setCaxSalesVolume(one7.getInAvgValue());
                gasNaturalInfo.setXsqySalesVolume(one8.getInAvgValue());
                gasNaturalInfo.setGasAllSalesVolume(one9.getInAvgValue());
                gasNaturalInfo.setGasUpstreamDemand(one10.getInAvgValue());
                gasNaturalInfo.setGasSupplyAmount(one11.getInAvgValue());
                gasNaturalInfo.setBjqSalesVolume(one12.getInAvgValue());
                gasNaturalInfo.setLaqSalesVolume(one13.getInAvgValue());
                gasNaturalInfo.setJdsSalesVolume(one14.getInAvgValue());
                gasNaturalInfo.setXsdcSalesVolume(one15.getInAvgValue());
                gasNaturalInfo.setOtherSalesVolume(one16.getInAvgValue());
                slaveGasNaturalInfo.save(gasNaturalInfo);

            }
        }
    }

    public void naturalInfo(){
        List<GasNaturalInfo> all = slaveGasNaturalInfo.findAll();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Timestamp timestamp = DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now());
        if (CollectionUtils.isEmpty(all)){
            List<StatistOtherParametersDay> list1 = dayRepository.findByType(gasUpstreamAllocation);
            List<StatistOtherParametersDay> list2 = dayRepository.findByType(gasPipeAmount);
            List<StatistOtherParametersDay> list3 = dayRepository.findByType(zcqSalesVolume);
            List<StatistOtherParametersDay> list4 = dayRepository.findByType(yhqSalesVolume);
            List<StatistOtherParametersDay> list5 = dayRepository.findByType(fyqSalesVolume);
            List<StatistOtherParametersDay> list6 = dayRepository.findByType(tlxSalesVolume);
            List<StatistOtherParametersDay> list7 = dayRepository.findByType(caxSalesVolume);
            List<StatistOtherParametersDay> list8 = dayRepository.findByType(xsqySalesVolume);
            List<StatistOtherParametersDay> list9 = dayRepository.findByType(gasAllSalesVolume);
            List<StatistOtherParametersDay> list10 = dayRepository.findByType(gasUpstreamDemand);
            List<StatistOtherParametersDay> list11 = dayRepository.findByType(gasSupplyAmount);
            List<StatistOtherParametersDay> list12 = dayRepository.findByType(bjqSalesVolume);
            List<StatistOtherParametersDay> list13 = dayRepository.findByType(laqSalesVolume);
            List<StatistOtherParametersDay> list14 = dayRepository.findByType(jdsSalesVolume);
            List<StatistOtherParametersDay> list15 = dayRepository.findByType(xsdcSalesVolume);
            List<StatistOtherParametersDay> list16 = dayRepository.findByType(otherSalesVolume);

            Map<String, Double> map1 = handleData(list1);
            Map<String, Double> map2 = handleData(list2);
            Map<String, Double> map3 = handleData(list3);
            Map<String, Double> map4 = handleData(list4);
            Map<String, Double> map5 = handleData(list5);
            Map<String, Double> map6 = handleData(list6);
            Map<String, Double> map7 = handleData(list7);
            Map<String, Double> map8 = handleData(list8);
            Map<String, Double> map9 = handleData(list9);
            Map<String, Double> map10 = handleData(list10);
            Map<String, Double> map11 = handleData(list11);
            Map<String, Double> map12 = handleData(list12);
            Map<String, Double> map13 = handleData(list13);
            Map<String, Double> map14 = handleData(list14);
            Map<String, Double> map15 = handleData(list15);
            Map<String, Double> map16 = handleData(list16);

            List<GasNaturalInfo> objects = new ArrayList<>();

            list1.forEach(v->{
                GasNaturalInfo gasNaturalInfo = new GasNaturalInfo();
                LocalDateTime lastTime = v.getLastTime();
                String key = timeFormatter.format(lastTime);
                if (map1.containsKey(key)){
                    gasNaturalInfo.setGasUpstreamAllocation(map1.get(key));
                }
                if (map2.containsKey(key)){
                    gasNaturalInfo.setGasPipeAmount(map2.get(key));
                }
                if (map3.containsKey(key)){
                    gasNaturalInfo.setZcqSalesVolume(map3.get(key));
                }
                if (map4.containsKey(key)){
                    gasNaturalInfo.setYhqSalesVolume(map4.get(key));
                }
                if (map5.containsKey(key)){
                    gasNaturalInfo.setFyqSalesVolume(map5.get(key));
                }
                if (map6.containsKey(key)){
                    gasNaturalInfo.setTlxSalesVolume(map6.get(key));
                }
                if (map7.containsKey(key)){
                    gasNaturalInfo.setCaxSalesVolume(map7.get(key));
                }
                if (map8.containsKey(key)){
                    gasNaturalInfo.setXsqySalesVolume(map8.get(key));
                }
                if (map9.containsKey(key)){
                    gasNaturalInfo.setGasAllSalesVolume(map9.get(key));
                }
                if (map10.containsKey(key)){
                    gasNaturalInfo.setGasUpstreamDemand(map10.get(key));
                }
                if (map11.containsKey(key)){
                    gasNaturalInfo.setGasSupplyAmount(map11.get(key));
                }
                if (map12.containsKey(key)){
                    gasNaturalInfo.setBjqSalesVolume(map12.get(key));
                }
                if (map13.containsKey(key)){
                    gasNaturalInfo.setLaqSalesVolume(map13.get(key));
                }
                if (map14.containsKey(key)){
                    gasNaturalInfo.setJdsSalesVolume(map14.get(key));
                }
                if (map15.containsKey(key)){
                    gasNaturalInfo.setXsdcSalesVolume(map15.get(key));
                }
                if (map16.containsKey(key)){
                    gasNaturalInfo.setOtherSalesVolume(map16.get(key));
                }
                gasNaturalInfo.setTongTime(timestamp);
                gasNaturalInfo.setBizTime(timestamp);
                gasNaturalInfo.setOp("insert");
                gasNaturalInfo.setDay(key);
                gasNaturalInfo.setDataTime(lastTime);
                objects.add(gasNaturalInfo);
            });
            slaveGasNaturalInfo.saveAll(objects);
        }else {
            LocalDateTime now = LocalDateTime.now();
            String format = timeFormatter.format(now);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dayOfMonthKey = LocalDate.parse(new Date().toString(), dateTimeFormatter).atStartOfDay();
            //如果已经同步过数据则查询前置库是否存在数据
            GasNaturalInfo byDay = slaveGasNaturalInfo.findByDay(format);
            if (null == byDay){
                GasNaturalInfo gasNaturalInfo = new GasNaturalInfo();
                //查询我们数据
                StatistOtherParametersDay byDayOfMonthKeyAndType1 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30029L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType2 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30027L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType3 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30011L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType4 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30013L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType5 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30015L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType6 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30017L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType7 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30024L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType8 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30026L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType9 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30002L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType10 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30030L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType11 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30008L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType12 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30012L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType13 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30016L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType14 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30018L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType15 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30025L, dayOfMonthKey);
                StatistOtherParametersDay byDayOfMonthKeyAndType16 = this.dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30019L, dayOfMonthKey);
                gasNaturalInfo.setGasUpstreamAllocation(byDayOfMonthKeyAndType1.getInAvgValue());
                gasNaturalInfo.setGasPipeAmount(byDayOfMonthKeyAndType2.getInAvgValue());
                gasNaturalInfo.setZcqSalesVolume(byDayOfMonthKeyAndType3.getInAvgValue());
                gasNaturalInfo.setYhqSalesVolume(byDayOfMonthKeyAndType4.getInAvgValue());
                gasNaturalInfo.setFyqSalesVolume(byDayOfMonthKeyAndType5.getInAvgValue());
                gasNaturalInfo.setTlxSalesVolume(byDayOfMonthKeyAndType6.getInAvgValue());
                gasNaturalInfo.setCaxSalesVolume(byDayOfMonthKeyAndType7.getInAvgValue());
                gasNaturalInfo.setXsqySalesVolume(byDayOfMonthKeyAndType8.getInAvgValue());
                gasNaturalInfo.setGasAllSalesVolume(byDayOfMonthKeyAndType9.getInAvgValue());
                gasNaturalInfo.setGasUpstreamDemand(byDayOfMonthKeyAndType10.getInAvgValue());
                gasNaturalInfo.setGasSupplyAmount(byDayOfMonthKeyAndType11.getInAvgValue());
                gasNaturalInfo.setBjqSalesVolume(byDayOfMonthKeyAndType12.getInAvgValue());
                gasNaturalInfo.setLaqSalesVolume(byDayOfMonthKeyAndType13.getInAvgValue());
                gasNaturalInfo.setJdsSalesVolume(byDayOfMonthKeyAndType14.getInAvgValue());
                gasNaturalInfo.setXsdcSalesVolume(byDayOfMonthKeyAndType15.getInAvgValue());
                gasNaturalInfo.setOtherSalesVolume(byDayOfMonthKeyAndType16.getInAvgValue());
                gasNaturalInfo.setTongTime(timestamp);
                gasNaturalInfo.setBizTime(timestamp);
                gasNaturalInfo.setOp("insert");
                gasNaturalInfo.setDay(format);
                gasNaturalInfo.setDataTime(dayOfMonthKey);
                slaveGasNaturalInfo.save(gasNaturalInfo);
            }

        }
    }
}
